<template>
  <div class="statistics-container">
    <!-- 数据筛选 -->
    <el-card class="filter-card">
      <div slot="header" class="filter-header">
        <span class="filter-title">数据筛选</span>
      </div>
      <el-row :gutter="20">
        <el-col :xs="24" :sm="8" :md="8">
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="dateRange.start"
              type="date"
              placeholder="选择开始日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="8" :md="8">
          <el-form-item label="结束日期">
            <el-date-picker
              v-model="dateRange.end"
              type="date"
              placeholder="选择结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="8" :md="8" class="filter-actions">
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="fetchData"
          >
            刷新数据
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 图表展示 -->
    <el-row :gutter="20" class="charts-row">
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card">
          <div slot="header" class="chart-header">
            <span class="chart-title">询标趋势</span>
          </div>
          <div ref="trendChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card">
          <div slot="header" class="chart-header">
            <span class="chart-title">回复率分析</span>
          </div>
          <div ref="replyRateChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card">
          <div slot="header" class="chart-header">
            <span class="chart-title">问题类型分布</span>
          </div>
          <div ref="questionTypeChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card">
          <div slot="header" class="chart-header">
            <span class="chart-title">响应时间分析</span>
          </div>
          <div ref="responseTimeChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import store from '@/store'

export default {
  name: 'Statistics',
  data() {
    return {
      dateRange: {
        start: new Date().toISOString().substr(0, 10),
        end: new Date().toISOString().substr(0, 10)
      },
      // 图表实例
      trendChart: null,
      replyRateChart: null,
      questionTypeChart: null,
      responseTimeChart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initCharts()
      this.fetchData()
      window.addEventListener('resize', this.handleResize)
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    this.trendChart?.dispose()
    this.replyRateChart?.dispose()
    this.questionTypeChart?.dispose()
    this.responseTimeChart?.dispose()
  },
  methods: {
    // 初始化图表
    initCharts() {
      // 询标趋势图
      this.trendChart = echarts.init(this.$refs.trendChartRef)
      this.trendChart.setOption({
        tooltip: { trigger: 'axis' },
        xAxis: { 
          type: 'category', 
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] 
        },
        yAxis: { type: 'value' },
        series: [{ 
          data: [120, 200, 150, 80, 70, 110, 130], 
          type: 'line', 
          smooth: true,
          itemStyle: { color: '#409EFF' }
        }]
      })

      // 回复率分析图
      this.replyRateChart = echarts.init(this.$refs.replyRateChartRef)
      this.replyRateChart.setOption({
        tooltip: { trigger: 'item' },
        legend: { orient: 'vertical', left: 'left' },
        series: [{
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: '已回复' },
            { value: 735, name: '未回复' }
          ],
          emphasis: { 
            itemStyle: { 
              shadowBlur: 10, 
              shadowOffsetX: 0, 
              shadowColor: 'rgba(0, 0, 0, 0.5)' 
            } 
          }
        }]
      })

      // 问题类型分布图
      this.questionTypeChart = echarts.init(this.$refs.questionTypeChartRef)
      this.questionTypeChart.setOption({
        tooltip: { trigger: 'item' },
        legend: { orient: 'vertical', left: 'left' },
        series: [{
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: '技术问题' },
            { value: 735, name: '商务问题' },
            { value: 580, name: '资质问题' },
            { value: 484, name: '其他问题' }
          ],
          emphasis: { 
            itemStyle: { 
              shadowBlur: 10, 
              shadowOffsetX: 0, 
              shadowColor: 'rgba(0, 0, 0, 0.5)' 
            } 
          }
        }]
      })

      // 响应时间分析图
      this.responseTimeChart = echarts.init(this.$refs.responseTimeChartRef)
      this.responseTimeChart.setOption({
        tooltip: { trigger: 'axis' },
        xAxis: { 
          type: 'category', 
          data: ['<1h', '1-2h', '2-4h', '4-8h', '8-24h', '>24h'] 
        },
        yAxis: { type: 'value', name: '数量' },
        series: [{ 
          data: [120, 200, 150, 80, 70, 110], 
          type: 'bar',
          itemStyle: { color: '#67C23A' }
        }]
      })
    },

    // 获取数据
    async fetchData() {
      try {
        const data = await store.dispatch('inquiry/getStatistics', {
          startDate: this.dateRange.start,
          endDate: this.dateRange.end
        })

        // 更新图表数据
        if (data) {
          this.trendChart.setOption({ series: [{ data: data.trendData }] })
          this.replyRateChart.setOption({ series: [{ data: data.replyRateData }] })
          this.questionTypeChart.setOption({ series: [{ data: data.questionTypeData }] })
          this.responseTimeChart.setOption({ series: [{ data: data.responseTimeData }] })
        }
        
        this.$message.success('数据刷新成功')
      } catch (error) {
        console.error('获取统计数据失败:', error)
        this.$message.error('获取统计数据失败')
      }
    },

    // 监听窗口大小变化
    handleResize() {
      this.trendChart?.resize()
      this.replyRateChart?.resize()
      this.questionTypeChart?.resize()
      this.responseTimeChart?.resize()
    }
  }
}
</script>

<style scoped>
.statistics-container {
  padding: 20px;
}

/* 筛选卡片样式 */
.filter-card {
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
}

.filter-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.12);
}

.filter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.filter-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.filter-actions {
  display: flex;
  align-items: flex-end;
  padding-bottom: 22px;
}

/* 图表行样式 */
.charts-row {
  margin-top: 0;
}

/* 图表卡片样式 */
.chart-card {
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  transition: box-shadow 0.3s ease;
}

.chart-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.12);
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chart-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.chart-container {
  height: 300px;
  width: 100%;
}

/* 表单项样式 */
.el-form-item {
  margin-bottom: 22px;
}

.el-form-item__label {
  font-weight: 500;
  color: #606266;
}

/* 日期选择器样式 */
.el-date-editor {
  width: 100%;
}

.el-date-editor .el-input__inner {
  border-radius: 4px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.el-date-editor .el-input__inner:focus {
  border-color: #409eff;
  outline: 0;
}

/* 按钮样式 */
.el-button {
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 4px;
}

.el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .statistics-container {
    padding: 10px;
  }
  
  .filter-actions {
    padding-bottom: 0;
    margin-top: 15px;
  }
  
  .el-button {
    width: 100%;
  }
  
  .chart-container {
    height: 250px;
  }
}

@media (max-width: 576px) {
  .chart-container {
    height: 200px;
  }
  
  .filter-title,
  .chart-title {
    font-size: 14px;
  }
}

/* 卡片内容区域 */
.el-card__body {
  padding: 20px;
}

.el-card__header {
  padding: 18px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #fafafa;
}

/* 栅格间距调整 */
.el-row {
  margin-left: -10px;
  margin-right: -10px;
}

.el-col {
  padding-left: 10px;
  padding-right: 10px;
}

/* 图表加载状态 */
.chart-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 300px;
  color: #909399;
  font-size: 14px;
}
</style>